package com.museum.guide.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.guide.common.exception.BusinessException;
import com.museum.guide.entity.Appointment;
import com.museum.guide.entity.AppointmentTimeSlot;
import com.museum.guide.mapper.AppointmentMapper;
import com.museum.guide.vo.TimeSlotStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AppointmentService extends ServiceImpl<AppointmentMapper, Appointment> {


    /**
     * 分页查询预约信息
     */
    public Page<Appointment> page(Page<Appointment> page, String visitorName, String status,
                                  LocalDate startDate, LocalDate endDate, Long userId) {
        LambdaQueryWrapper<Appointment> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(visitorName), Appointment::getVisitorName, visitorName)
                .eq(StringUtils.hasText(status), Appointment::getStatus, status)
                .eq(userId != null, Appointment::getUserId, userId)
                .ge(startDate != null, Appointment::getAppointmentDate, startDate)
                .le(endDate != null, Appointment::getAppointmentDate, endDate)
                .orderByDesc(Appointment::getCreateTime);
        return page(page, wrapper);
    }

    /**
     * 获取用户的预约列表
     */
    public List<Appointment> getUserAppointments(Long userId) {
        LambdaQueryWrapper<Appointment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Appointment::getUserId, userId)
                .orderByDesc(Appointment::getCreateTime);
        return list(wrapper);
    }

    /**
     * 新增预约
     */
    public void saveAppointment(Appointment appointment) {
        appointment.setStatus("PENDING");
        appointment.setCreateTime(LocalDateTime.now());
        appointment.setUpdateTime(LocalDateTime.now());
        save(appointment);
    }

    /**
     * 更新预约信息
     */
    public void updateAppointment(Appointment appointment) {
        appointment.setUpdateTime(LocalDateTime.now());
        updateById(appointment);
    }

    /**
     * 取消预约
     */
    public void cancelAppointment(Long id) {
        Appointment appointment = getById(id);
        if (appointment != null) {
            appointment.setStatus("CANCELED");
            appointment.setUpdateTime(LocalDateTime.now());
            updateById(appointment);
        }
    }

    /**
     * 审核预约
     */
    public void reviewAppointment(Long id, String status, String remark) {
        Appointment appointment = getById(id);
        if (appointment != null) {
            appointment.setStatus(status);
            appointment.setRemark(remark);
            appointment.setUpdateTime(LocalDateTime.now());
            updateById(appointment);
        }
    }

    public List<TimeSlotStatistics> getTimeSlotStatistics(String date) {
        return this.baseMapper.getTimeSlotStatistics(date);
    }
}